package com.elink.enginerring.library;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elink.enginerring.common.Constant;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.label.entity.Label;
import com.elink.enginerring.label.service.ILabelService;
import com.elink.enginerring.user.entity.User;
import com.elink.enginerring.util.JSONUtil;

@Controller
@RequestMapping("view/thesis/label")
@Api(value = "论文标签接口")
@ResponseBody
public class LibLabelController {
	@Autowired
	private ILabelService labelService;

	/**
	 * 获取标签list
	 * @return
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	@ApiOperation(value = "获取标签list", httpMethod = "GET", notes = "获取标签list")
	public Map<String, Object> getList() {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return labelService.getList();
	}

	/**
	 * 添加标签
	 * @param label
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "添加标签", httpMethod = "POST", notes = "添加标签")
	public Map<String, Object> add(@ModelAttribute @Valid Label label,HttpSession session) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		User user = (User) session.getAttribute(Constant.su_in_session);
		label.setUserId(user.getUserId());
		return labelService.add(label);
	}
	
	
	/**
	 * 批量添加标签
	 * @return
	 */
	@RequestMapping(value = "/addBatch", method = RequestMethod.POST)
	@ApiOperation(value="批量添加标签",httpMethod="POST",notes="批量添加标签")
	public Map<String, Object>addBatch(@ApiParam(value="标签集合，以逗号进行分割")
	@RequestParam(value="tags",required=true)String tags,HttpSession session){
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());

		User user=(User) session.getAttribute(Constant.su_in_session);
		
		return JSONUtil.getJSON(ErrorType.SUCCESS,labelService.addBatch(tags,user.getUserId()));
	}
	
	/**
	 * 修改标签
	 * @param label
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ApiOperation(value = "修改标签", httpMethod = "POST", notes = "修改标签")
	public Map<String, Object> modify(@ModelAttribute @Valid Label label) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return labelService.modify(label);
	}
	
	/**
	 * 删除标签
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	@ApiOperation(value = "删除标签", httpMethod = "GET", notes = "删除标签")
	public Map<String, Object> del(@ApiParam(value="标签主键ids，比如'1,2'",required=true)
	@RequestParam(value="ids",required=true) String ids) {
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return labelService.del(ids);
	}
	
	/**
	 * 挤压式排序
	 * @param id_seq
	 * @return
	 */
	@RequestMapping(value="/order",method=RequestMethod.GET)
	@ApiOperation(value = "排序", httpMethod = "GET", notes = "挤压式排序")
	public Map<String, Object> order(@RequestParam("id_seq") String id_seq){
		
		DataSourceContextHolder.setDbType(DataSourceType.DBLIBRARY);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return labelService.order(id_seq);
	}
}
